package com.vitabuybuy.tv.ui.tencent.liveroom.roomutil.widget

import android.content.Context
import android.graphics.Color
import android.view.View
import android.view.animation.Animation
import androidx.core.content.ContextCompat
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.library.common.base.BindRecycAdapter
import com.library.common.toast.XToast
import com.library.common.util.DensityUtil
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.ItemGiftBinding
import com.vitabuybuy.tv.databinding.MlvbDialogGiftListBinding
import com.vitabuybuy.tv.ui.tencent.liveroom.bean.GiftListBean
import razerdp.basepopup.BasePopupWindow
import razerdp.util.animation.AnimationHelper
import razerdp.util.animation.TranslationConfig

class GiftListDialog(context: Context) : BasePopupWindow(context) {
    private lateinit var mAdapter: GiftAdapter

    private var mCoinValue: Int? = 0

    private lateinit var mBinding: MlvbDialogGiftListBinding

    override fun onCreateContentView(): View {
        return createPopupById(R.layout.mlvb_dialog_gift_list)
    }

    override fun onViewCreated(contentView: View) {
        super.onViewCreated(contentView)
        mBinding = DataBindingUtil.bind<ViewDataBinding>(contentView) as MlvbDialogGiftListBinding
        mAdapter = GiftAdapter()
        mBinding.lvGift.apply {
            adapter = mAdapter
            layoutManager = GridLayoutManager(context, 4)
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            for (data in mAdapter.mData) {
                data.isChecked = false
            }
            mAdapter.mData[position].isChecked = true
            mAdapter.notifyDataSetChanged()
        }
        mBinding.btnChongZhi.setOnClickListener {
            onCliclRechargeListener.invoke(mCoinValue ?: 0)
            dismiss()
        }
        mBinding.btnQuality.setOnClickListener {
            GiftQuantityPopup(
                context
            ) { quantity ->
                mBinding.btnQuality.text = quantity
            }.showPopupWindow()
        }
        mBinding.btnSend.setOnClickListener {
            if (mCoinValue == 0) {
                XToast.normal(context.getString(R.string.you_dont_have_enough_bounty))
                return@setOnClickListener
            }
            var bean: GiftListBean? = null
            for (data in mAdapter.mData) {
                if (data.isChecked) {
                    bean = data
                }
            }
            bean?.run {
                val shouldPrice = this.price * mBinding.btnQuality.text.toString().toInt()
                if (shouldPrice > (mCoinValue ?: 0)) {
                    XToast.normal(context.getString(R.string.you_dont_have_enough_bounty))
                    return@setOnClickListener
                }
                onClicSendkListener.invoke(bean, mBinding.btnQuality.text.toString().toInt())
                dismiss()
            } ?: let {
                XToast.normal(context.getString(R.string.plsease_choose_gift))
                return@setOnClickListener
            }
        }

    }

    fun initData(giftList: MutableList<GiftListBean>, mCoinValue: Int?) {
        mAdapter.setNewData(giftList)
        this.mCoinValue = mCoinValue
        mBinding.tvGold.text = this.mCoinValue.toString()
    }

    override fun onCreateShowAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.FROM_BOTTOM)
            .toShow()
    }

    override fun onCreateDismissAnimation(): Animation {
        return AnimationHelper.asAnimation()
            .withTranslation(TranslationConfig.TO_BOTTOM)
            .toDismiss()
    }

    class GiftAdapter : BindRecycAdapter<ItemGiftBinding, GiftListBean>() {
        override fun normalViewId(viewType: Int): Int {
            return R.layout.item_gift
        }

        override fun onBindViewHolder(
            mBinding: ItemGiftBinding,
            itemData: GiftListBean,
            holder: RecyclerView.ViewHolder,
            position: Int
        ) {
            mBinding.llGift.setBackgroundResource(if (itemData.isChecked) R.drawable.rect_0_all_red else R.drawable.rect_0_all_00000000)
            Glide.with(mContext)
                .asBitmap().load(itemData.icon).centerCrop()
                .placeholder(ContextCompat.getDrawable(mContext, R.drawable.icon_3_jubao))
                .into(mBinding.ivGift)
            mBinding.tvGold.text = itemData.price.toString() + "币"
            mBinding.tvName.text = itemData.name
        }
    }

    lateinit var onCliclRechargeListener: (mCoin: Int) -> Unit
    lateinit var onClicSendkListener: (giftId: GiftListBean, quantity: Int) -> Unit
}